package cn.szzsi._web;

import cn.szzsi.core.kit.UtilDate;
import cn.szzsi.service.UserBalanceService;
import cn.szzsi.service.UserPointDetailService;
import cn.szzsi.common.bean.dto.Msg;

import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.UserPointDetail;
import cn.szzsi.common.model.UserSing;
import com.jfinal.aop.Clear;

import java.util.Date;

/**
 * Created by haotian on 2016/12/12.
 */
public class UserSignController extends BaseWebController<UserSing> {
    private Integer point;
    private String remark;
    private Integer continueSingDays0;//买家连续签到天数
    @Clear
    public void sign(){
        String shopId=getPara("shopId");
        Member member=getLoginMember();
        //根据shopId查询商户设置的而签到奖励信息
        SiteConfig siteConfig= SiteConfig.dao.findByShopId(shopId);
        int firstSignPoint=siteConfig.getInt("firstSignPoint");//首次签到奖励分数
        int signPoint=siteConfig.getInt("signPoint");//后续每次签到奖励积分数
        int continueSetDays=siteConfig.getInt("continueSingDays");//连续签到多少天节点
        int extraContinuePoint=siteConfig.getInt("extraContinuePoint");//连续签到多少天额外增加积分
        if(UserPointDetailService.service.isFirstSign(member.getStr("id"))){//首次签到
            UserBalanceService.service.updatePoint(member,firstSignPoint);//更新会员余额结果
            UserPointDetailService.service.saveSignInfo(member.getStr("id"),firstSignPoint, UserPointDetail.PointDetailType.memberSign.ordinal(),"会员首次签到",1);//积分交易明细

        }else{
            UserPointDetail lastSign= UserPointDetailService.service.getLastSignRecord(member.getStr("id"));//查询买家最后一次签到记录
            Date date=lastSign.getDate("createDate");//买家最后一次签到时间
            int continueSingDays=lastSign.getInt("continueSingDays");//连续签到天数
            Long lastDateTime=date.getTime();//最后一次签到时间戳
            Long todatStartTime= UtilDate.getTodayStartTime();//当天凌晨时间戳
            if(todatStartTime<lastDateTime){//已经签到
                renderJson(Msg.fail("亲,您今天已经签到啦!",""));
                return;
            }
            //判断昨天有没有签到
            if(lastDateTime>=UtilDate.getYestdayStartTime() && lastDateTime<UtilDate.getTodayStartTime()){//昨天签到
                if((continueSingDays+1)>=continueSetDays){//签到次数大于或等于卖家设置的天数
                    //UserSing.dao.saveSignRecord(signPoint+extraContinuePoint,continueSingDays+1,"正常签到+额外签到奖励",member.getStr("id"),shopId );
                   //member.set("point",member.getInt("point")+signPoint+extraContinuePoint).update();

                    UserBalanceService.service.updatePoint(member,signPoint+extraContinuePoint);//更新会员余额结果
                    UserPointDetailService.service.saveSignInfo(member.getStr("id"),signPoint+extraContinuePoint, UserPointDetail.PointDetailType.memberSign.ordinal(),"正常签到+额外签到奖励",continueSingDays+1);//积分交易明细
                }else{//正常签到
                    //UserSing.dao.saveSignRecord(signPoint,continueSingDays+1,"正常签到",member.getStr("id"),shopId );
                    //member.set("point",member.getInt("point")+signPoint).update();

                    UserBalanceService.service.updatePoint(member,signPoint);//更新会员余额结果
                    UserPointDetailService.service.saveSignInfo(member.getStr("id"),signPoint, UserPointDetail.PointDetailType.memberSign.ordinal(),"正常签到",continueSingDays+1);//积分交易明细
                }
            }else{//昨天没签到,将连续签到天数改为1
                //UserSing.dao.saveSignRecord(signPoint,1,"中断签到",member.getStr("id"),shopId);
                //member.set("point",member.getInt("point")+signPoint).update();
                UserBalanceService.service.updatePoint(member,signPoint);//更新会员余额结果
                UserPointDetailService.service.saveSignInfo(member.getStr("id"),signPoint, UserPointDetail.PointDetailType.memberSign.ordinal(),"中断签到",1);//积分交易明细
            }
        }
        renderJson(Msg.SUCCESS);
    }
}
